let Vue;
class VueRouter{
    constructor(options){
        this.$options = options
        // 定义响应式的属性current
        const initial = window.location.hash.slice(1) || '/'
        Vue.util.defineReactive(this, 'current', initial)
        // 监听hashchange事件
        window.addEventListener('hashchange', this.onHashChange.bind(this))

        // 缓存path和route映射关系
        this.routeMap = {}
        this.$options.routes.forEach(route => {
            this.routeMap[route.path] = route
        });
    }
    onHashChange() { 
        this.current = window.location.hash.slice(1)
    }
}

VueRouter.install = function(_Vue){
    Vue = _Vue
    console.log(Vue)

    Vue.mixin({
        beforeCreate() {
            if(this.$options.router){
                Vue.prototype.$router = this.$options.router
            }
        }
    })

    // 任务2：实现两个全局组件router-link和router-view
    Vue.component('router-link', ()=> import('./router-link'))
    Vue.component('router-view', () => import('./router-view'))
}
export default VueRouter